﻿/*----------------------------------------------------------------
// Copyright (C) 2009 
// 版权所有。 
//
// 文件名：
// 文件功能描述：客户信息管理
//
//
// 创建标识：
// 程序作者：翟允锋
// 修改标识：
// 修改描述：
//----------------------------------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EQT.Dal;
using System.Threading;
using EQT.Model;

public partial class Client_Info : System.Web.UI.Page
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private DaClientInfo dalClent = new DaClientInfo();
    private UserContext context;

    protected void Page_Load(object sender, EventArgs e)
    {
        context = Session["USER_CONTEXT"] as UserContext;
        OperationResult.Text = "";
        divResult.Visible = false;

        if (!IsPostBack)
        {
            AspNetPager.PageSize = 15;
            this.BtnQuery_Click(null, null);

            this.SetPanelVisiable(SummaryPanel);
        }
    }

    protected void AspNetPager_PageChanged(object sender, EventArgs e)
    {
        this.BtnQuery_Click(null, null);
    }

    protected void EditSave_Click(object sender, EventArgs e)
    {
        //执行保存操作。
        MoClientInfo mod = new MoClientInfo();

        try
        {
            mod.Client_Id = EditClient_ID.Text;

            mod.Addr = EditAddr.Text.Trim();
            mod.Consigner = EditConsigner.Text.Trim();
            mod.Corp_Name = EditCorp_Name.Text.Trim();
            mod.Email = Editemail.Text.Trim();
            mod.Fax = EditFax.Text.Trim();
            mod.Linkman = EditLinkman.Text.Trim();
            mod.Phone = EditPhone.Text.Trim();
            mod.Postcode = EditPostcode.Text.Trim();
            mod.Pwd = EditPWD.Text.Trim();
            mod.Tel = EditTel.Text.Trim();
            mod.Update_Time = DateTime.Now;

            int rtn = dalClent.Update(mod);

            if (rtn == 1)
            {
                this.BtnQuery_Click(null, null);
                OperationResult.Text = "客户信息修改成功！"; divResult.Visible = true;
                divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "客户信息修改失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("客户信息修改失败", ex);
            OperationResult.Text = "客户信息修改失败！"; divResult.Visible = true;
        }
        
    }

    protected void EditBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void CreateSave_Click(object sender, EventArgs e)
    {
        //执行保存操作。
        MoClientInfo mod = new MoClientInfo();

        try
        {
            mod.Client_Id = SequenceKeys.GenerateClientID();
            mod.Addr = CreateAddr.Text.Trim();
            mod.Consigner = CreateConsigner.Text.Trim();
            mod.Corp_Name = CreateCorp_Name.Text.Trim();
            mod.Create_Time = DateTime.Now;
            mod.Email = Createemail.Text.Trim();
            mod.Fax = CreateFax.Text.Trim();
            mod.Linkman = CreateLinkman.Text.Trim();
            mod.Phone = CreatePhone.Text.Trim();
            mod.Postcode = CreatePostcode.Text.Trim();
            mod.Pwd = CreatePWD.Text.Trim();
            mod.Tel = CreateTel.Text.Trim();
            mod.Update_Time = DateTime.Now;

            int rtn = dalClent.Add(mod);

            if (rtn == 1)
            {
                this.BtnQuery_Click(null, null);
                OperationResult.Text = "客户信息创建成功！"; divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "客户信息创建失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("客户信息创建失败", ex);
            OperationResult.Text = "客户信息创建失败！"; divResult.Visible = true;
        }
    }

    protected void CreateBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void CreateGo_Click(object sender, EventArgs e)
    {
        this.CreateCancel();
        this.SetPanelVisiable(CreatePanel);
    }

    protected void DetailBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void EditGo_Click(object sender, EventArgs e)
    {
        string strKey = DetailClient_ID.Text;
        this.DisplayEditInfo(strKey);
        this.SetPanelVisiable(EditPanel);
    }

    protected void DeleteGo_Click(object sender, EventArgs e)
    {
        string strKey = DetailClient_ID.Text.Trim();
        try
        {
            int rtn = this.DeleteInfo(strKey);
            if (rtn == 1)
            {
                this.BtnQuery_Click(null, null);
                OperationResult.Text = "客户信息删除成功！"; divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "客户信息删除失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("客户信息删除失败", ex);
            OperationResult.Text = "客户信息删除失败！"; divResult.Visible = true;
        }
    }

    protected void GvDelete_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = gvr.Cells[0].Text.Trim();
        try
        {
            int rtn = this.DeleteInfo(strKey);

            if (rtn == 1)
            {
                this.BtnQuery_Click(null, null);
                OperationResult.Text = "客户信息删除成功！"; divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "客户信息删除失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("客户信息删除失败", ex);
            OperationResult.Text = "客户信息删除失败！"; divResult.Visible = true;
        }
    }

    protected void GvEdit_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = gvr.Cells[0].Text.Trim();

        this.DisplayEditInfo(strKey);
        this.SetPanelVisiable(EditPanel);
    }

    protected void GvDetail_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = gvr.Cells[0].Text.Trim();

        this.DisplayDetailInfo(strKey);
        this.SetPanelVisiable(DetailPanel);
    }

    protected void SetPanelVisiable(Panel panel)
    {
        EditPanel.Visible = false;
        DetailPanel.Visible = false;
        CreatePanel.Visible = false;
        SummaryPanel.Visible = false;

        panel.Visible = true;
    }

    protected void BtnQuery_Click(object sender, EventArgs e)
    {
        int pagecount = 0;
        //这里查询员工信息并绑定到gridview上。
        string strCondition = "where 1=1 ";
        if (sender != null && e != null)
        {
            AspNetPager.CurrentPageIndex = 0;
        }
        if (!string.IsNullOrEmpty(TxtClient_ID.Text.Trim()))
        {
            strCondition = strCondition + "and  Client_ID='" + TxtClient_ID.Text.Trim() + "'";
        }

        if (!string.IsNullOrEmpty(TxtCorp_Name.Text.Trim()))
        {
            strCondition = strCondition + "and  Corp_Name like '%" + TxtCorp_Name.Text.Trim() + "%'";
        }

        this.ClientGridView.DataSource = dalClent.GetClientRecords(strCondition, AspNetPager.PageSize, AspNetPager.CurrentPageIndex - 1, out pagecount);

        ClientGridView.DataBind();

        AspNetPager.RecordCount = pagecount;
        this.SetPanelVisiable(SummaryPanel);
    }

    protected int DeleteInfo(string strKey)
    {
        //执行删除操作。
        return dalClent.Delete(strKey);
    }

    protected void DisplayDetailInfo(string strKey)
    {
        //这里查询员工信息
        MoClientInfo mod = dalClent.GetEntity(strKey);

        //然后给查看视图赋值。
        DetailClient_ID.Text = mod.Client_Id;
        DetailCorp_Name.Text = mod.Corp_Name;
        DetailConsigner.Text = mod.Consigner;
        DetailLinkman.Text = mod.Linkman;
        DetailTel.Text = mod.Tel;
        DetailPhone.Text = mod.Phone;
        DetailFax.Text = mod.Fax;
        DetailAddr.Text = mod.Fax;
        DetailPostcode.Text = mod.Postcode;
        Detailemail.Text = mod.Email;
        DetailPWD.Text = mod.Pwd;
    }

    protected void DisplayEditInfo(string strKey)
    {
        //这里查询员工信息
        MoClientInfo mod = dalClent.GetEntity(strKey);

        //然后给编辑视图赋值。
        EditClient_ID.Text = mod.Client_Id;
        EditCorp_Name.Text = mod.Corp_Name;
        EditConsigner.Text = mod.Consigner;
        EditLinkman.Text = mod.Linkman;
        EditTel.Text = mod.Tel;
        EditPhone.Text = mod.Phone;
        EditFax.Text = mod.Fax;
        EditAddr.Text = mod.Fax;
        EditPostcode.Text = mod.Postcode;
        Editemail.Text = mod.Email;
        EditPWD.Text = mod.Pwd;
    }

    protected void CreateCancel()
    {
        CreateClient_ID.Text = "";
        CreateCorp_Name.Text = "";
        CreateConsigner.Text = "";
        CreateLinkman.Text = "";
        CreateTel.Text = "";
        CreatePhone.Text = "";
        CreateFax.Text = "";
        CreateAddr.Text = "";
        CreatePostcode.Text = "";
        Createemail.Text = "";
        CreatePWD.Text = "";
    }
}
